
package com.eyedog.library.utils;

import android.content.Context;

import java.lang.reflect.Field;

public class StatusBarUtils {

    private static int sStatusBarHeight = 0;

    public static int getStatusBarHeight(Context context) {
        if (sStatusBarHeight > 0) {
            return sStatusBarHeight;
        }
        int result = 0;
        try {
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
                    "android");
            if (resourceId > 0) {
                result = context.getResources().getDimensionPixelSize(resourceId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (result <= 0) {
            try {
                Class<?> c = Class.forName("com.android.internal.R$dimen");
                Object o = c.newInstance();
                Field field = c.getField("status_bar_height");
                int id = (Integer) field.get(o);
                result = context.getResources().getDimensionPixelSize(id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (result <= 0) {
            result = DensityUtils.dp2px(context, 25);
        }
        sStatusBarHeight = result;
        return result;
    }
}
